package com.forever7776.life.web.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.forever7776.life.core.common.constants.GlobalConstants;
import com.forever7776.life.core.entity.blog.Blog;
import com.forever7776.life.core.entity.sys.SysUser;
import com.forever7776.life.core.service.blog.IBlogService;
import com.forever7776.life.core.service.sys.ISysUserService;
import com.forever7776.life.remote.common.vo.ResultVO;
import com.forever7776.life.remote.rpc.SendEmailRpcService;
import enums.ResultEnum;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 项目首页
 *
 * @author kz
 * @date 2017年12月1日14:32:07
 */
@Controller
@RequestMapping(value = "/")
public class GlobalController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IBlogService iBlogService;

    /**
     * 首页
     *
     * @return
     */
    @RequestMapping
    public String index(ModelMap map) {
        map.put("carouselBlogs", iBlogService.selectCarouselBlogs(new Page<Blog>()).getRecords());
        map.put("blogActiveUsers", iBlogService.selectBlogActiveUser(2));
        return FRONT_VIEW_PAGE_PATH_LIFES + "front/index";
    }


    /**
     * 到登录页面
     *
     * @return
     */
    @RequestMapping("login")
    public String login() {
        return "login/life/login";
    }


    /**
     * 到注册页面
     *
     * @return
     */
    @RequestMapping("register")
    public String register() {
        return "login/life/register";
    }

    /**
     * 忘记登录名(如:jack)
     *
     * @return
     */
    @RequestMapping("forgetUserName")
    public String forgetUserName() {
        return "front/register";
    }

    /**
     * 忘记密码
     *
     * @return
     */
    @RequestMapping("forgetPassword")
    public String forgetPassword() {
        return "front/register";
    }

    /**
     * 注册
     *
     * @param sysUser
     * @return
     */
    @RequestMapping(value = "register", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO register(SysUser sysUser) {
        ResultVO result = sysUserService.checkRegisterForm(sysUser);
        if (!result.getCode().equals(ResultEnum.STATUS.SUCCESS.getCode())) {
            return result;
        }
        sysUser.setHeadimg(GlobalConstants.USER_HEAD_DEFAULT);
        sysUser.setSalt(DigestUtils.md5Hex(sysUser.getPassword()));
        sysUserService.insert(sysUser);
        return result;
    }

    /**
     * ajax登录请求
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> submitLogin(String username, String password, Boolean rememberMe, Model model) {
        long begin = System.currentTimeMillis();
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>(2);
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(username, password, false);
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
            subject.getSession().setTimeout(1000 * 60 * 60);
            resultMap.put("status", 200);
            resultMap.put("message", "登录成功~");
            logger.info("登录成功！，耗时：{}", (System.currentTimeMillis() - begin));
        } catch (Exception e) {
            resultMap.put("status", 500);
            resultMap.put("message", e.getMessage());
        }
        return resultMap;
    }

    /**
     * 退出
     *
     * @return
     */
    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public String logout() {
        try {
            //退出
            sysUserService.updateLogoutTime(getUserName());
            SecurityUtils.getSubject().logout();
        } catch (Exception e) {
            logger.warn("登出系统异常");
        }
        return "redirect:/";
    }

}
